package cn.swust.service.serviceimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import cn.swust.model.OrderBean;
import cn.swust.service.OrderService;
import cn.swust.util.DBUtil;

public class OrderServiceImpl implements OrderService {

	public int addOrder(OrderBean ord) {
		int ret = 0;
		try{
		String insertSql = "insert into orderInfo(order_num,order_products,order_consignee,order_time" + 
		",order_allprice,order_status,order_state,order_ip,order_address,user_id,order_man) values ('" +
		ord.getNum() + "','" + ord.getProducts() + "','" + ord.getConsignee() + "','"  +
		ord.getTime() + "'," + ord.getAllPrice() + "," + ord.getStatus() +",'" + ord.getState() +
		"','" + ord.getIp() + "','" + ord.getAddress() + "'," + ord.getUseId() +
		",'" + ord.getMan() + "')";
		Connection conn = DBUtil.getConnection();
		Statement state = conn.createStatement();
		state.execute(insertSql, Statement.RETURN_GENERATED_KEYS);
		ResultSet rs = state.getGeneratedKeys();
		while(rs.next())
			ret = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return ret;
	}

	public int delOrder(int id) {
		String delSql = "delete from orderInfo where order_id = " + id;
		return DBUtil.updateSql(delSql);
	}

	public int modOrder(int id, OrderBean o) {
		String modSql = "update orderInfo set order_num = '" + o.getNum() + 
		"',order_products = '" + o.getProducts() + "',order_consignee = '" +
		o.getConsignee() + "',order_time = '" + o.getTime() + "',order_allprice = " +
		o.getAllPrice() + ",order_status = " + o.getStatus() + ",order_state  = '" + o.getState() +
		"',order_ip = '" + o.getIp() + "',order_address = '" + o.getAddress() +
		"',user_id = " + o.getUseId() + ",order_man = '" + o.getMan() +
		"' where order_id = " + id;
		return DBUtil.updateSql(modSql);
	}

	public OrderBean queryById(int id) {
		String insertSql = "select * from orderInfo where order_id = " + id;
		ResultSet rs = DBUtil.querySql(insertSql, DBUtil.creatStatement(DBUtil.getConnection()));
		OrderBean ord = new OrderBean();
		try {
			if (rs.next()) {
				ord.setId(rs.getInt("order_id"));
				ord.setNum(rs.getString("order_num"));
				ord.setProducts(rs.getString("order_products"));
				ord.setConsignee(rs.getString("order_consignee"));
				ord.setTime(rs.getString("order_time"));
				ord.setAllPrice(rs.getFloat("order_allprice"));
				ord.setStatus(rs.getInt("order_status"));
				ord.setState(rs.getString("order_state"));
				ord.setIp(rs.getString("order_ip"));
				ord.setAddress(rs.getString("order_address"));
				ord.setUseId(rs.getInt("user_id"));
				ord.setMan(rs.getString("order_man"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ord;
	}

	public OrderBean queryByNum(String num) {
		String insertSql = "select * from orderInfo where order_num = '" + num + "'";
		ResultSet rs = DBUtil.querySql(insertSql, DBUtil.creatStatement(DBUtil.getConnection()));
		OrderBean ord = new OrderBean();
		try {
			if (rs.next()) {
				ord.setId(rs.getInt("order_id"));
				ord.setNum(rs.getString("order_num"));
				ord.setProducts(rs.getString("order_products"));
				ord.setConsignee(rs.getString("order_consignee"));
				ord.setTime(rs.getString("order_time"));
				ord.setAllPrice(rs.getFloat("order_allprice"));
				ord.setStatus(rs.getInt("order_status"));
				ord.setState(rs.getString("order_state"));
				ord.setIp(rs.getString("order_ip"));
				ord.setAddress(rs.getString("order_address"));
				ord.setUseId(rs.getInt("user_id"));
				ord.setMan(rs.getString("order_man"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ord;
	}

	public ArrayList<OrderBean> queryByUser(int userId) {
		String insertSql = "select * from orderInfo where user_id = " + userId;
		ResultSet rs = DBUtil.querySql(insertSql, DBUtil.creatStatement(DBUtil.getConnection()));
		ArrayList<OrderBean> ordList = new ArrayList<OrderBean>();
		try {
			while (rs.next()) {
				OrderBean ord = new OrderBean();
				ord.setId(rs.getInt("order_id"));
				ord.setNum(rs.getString("order_num"));
				ord.setProducts(rs.getString("order_products"));
				ord.setConsignee(rs.getString("order_consignee"));
				ord.setTime(rs.getString("order_time"));
				ord.setAllPrice(rs.getFloat("order_allprice"));
				ord.setStatus(rs.getInt("order_status"));
				ord.setState(rs.getString("order_state"));
				ord.setIp(rs.getString("order_ip"));
				ord.setAddress(rs.getString("order_address"));
				ord.setUseId(rs.getInt("user_id"));
				ord.setMan(rs.getString("order_man"));
				ordList.add(ord);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ordList;
	}

	public int delOrderBiNum(String num) {
		String delSql = "delete from orderInfo where order_num = '" + num + "'";
		return DBUtil.updateSql(delSql);
	}

}
